//
//  ZKTransitionInteractiveConfig.m
//  朱凯的工具箱
//
//  Created by pro on 17/5/12.
//  Copyright © 2017年 zhukai. All rights reserved.
//

#import "KZTransitionInteractiveConfig.h"



@implementation KZTransitionInteractiveConfig
{
    dispatch_block_t                    _operationBlock;    // 操作block
    CGFloat                             _percent;           // 进度
    
    KZTransitionInteractiveDirection    _direction;
    KZTransitionInteractiveGestureType  _gestureType;
    CGFloat                             _usefulSize;
}

- (instancetype)initWithOperation:(dispatch_block_t)operationBlock
                    gestureConfig:(void(^)(KZGestureConfigFetch fetch))gestureConfig {

    if (self = [super init]) {
        
        _operationBlock = operationBlock;
        _percent = 0;
        if (gestureConfig) {
            KZGestureConfigFetch fetch = ^(KZTransitionInteractiveGestureType type, KZTransitionInteractiveDirection dir, CGFloat usefulSize) {
                
                self->_gestureType = type;
                self->_direction = dir;
                self->_usefulSize = usefulSize;
            };
            gestureConfig(fetch);
        }
    }
    return self;
}

- (void)addGestureToView:(UIView *)view {
    
    UIGestureRecognizer *gesture = nil;
    switch (_gestureType) {
        case KZTransitionInteractiveGestureTypeEdgePan:
        {
            gesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanAction:)];
            switch (_direction) {
                case KZTransitionInteractiveDirectionLeft:
                    ((UIScreenEdgePanGestureRecognizer *)gesture).edges = UIRectEdgeLeft;
                    break;
                case KZTransitionInteractiveDirectionRight:
                    ((UIScreenEdgePanGestureRecognizer *)gesture).edges = UIRectEdgeRight;
                    break;
                case KZTransitionInteractiveDirectionUp:
                    ((UIScreenEdgePanGestureRecognizer *)gesture).edges =
                        UIRectEdgeTop;
                    break;
                case KZTransitionInteractiveDirectionDown:
                    ((UIScreenEdgePanGestureRecognizer *)gesture).edges = UIRectEdgeBottom;
                    break;
                default:
                    break;
            }
        }
            break;
        case KZTransitionInteractiveGestureTypeNormalPan:
        {
            // TODO: 待拓展
        }
            break;
        default:
            break;
    }
    [view addGestureRecognizer:gesture];
}
- (void)edgePanAction:(UIScreenEdgePanGestureRecognizer *)pan {
    
    CGPoint point = [pan translationInView:pan.view];
    NSLog(@"%@", NSStringFromCGPoint(point));
    CGFloat offsetX = point.x;
    switch (pan.state) {
        case UIGestureRecognizerStateBegan:
        {
            _interation = YES;
            if (_operationBlock) {
                _operationBlock();
            }
            [self updateInteractiveTransition:_percent];
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            _percent = offsetX / SCREEN_WIDTH;
            [self update];
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            [self cancel];
        }
            break;
            
        default:
            break;
    }
}

- (void)update {
    
    [self updateInteractiveTransition:_percent];
}
- (void)cancel {
    
    if (_percent >= 0.5) {
        [self finish];
    } else {
        [self cancelInteractiveTransition];
        _percent = 0;
        _interation = NO;
    }
}
- (void)finish {
    
    [self finishInteractiveTransition];
    _percent = 0;
    _interation = NO;
}

@end
